
import scrollOpt from '../base/scrollOptimize'
import { UI_Layer } from '../../config/ui_def'
import { WX_AD_CONFIG, WX_VER_CONFIG, Sdk_Platform } from '../../frame/constants';
cc.Class({
    extends: scrollOpt,

    properties: {
        clickNode: cc.Node,
        bannerNode: cc.Node
    },
    initPage() {
        if (!he.pool.getPool('adIcon')) {
            he.pool.createPoolSync('adIcon', 10, cc.Prefab, 'uimod/adIcon')
        }
        if (!he.pool.getPool('adIconSec')) {
            he.pool.createPoolSync('adIconSec', 5, cc.Prefab, 'uimod/adIconSec')
        }
    },

    updatePage(param) {
        this.isTop = true
        this.time = 0
        this.canUpdate = true
        this.scrollClick = false
        this.timeToScroll = this.randomNum(200, 300)
        this.scrollInit()
        this.scroll.scrollToTop()
        // gg.eventManager.on('roleInfo', this.listenClient, this)
        this.clickNode.active = true
        this.clickNode.on(cc.Node.EventType.TOUCH_START, this.scrollServer, this)
        this.creatAdList(param.imgList)
        this.isShowBanner = false
        // this.bannerInit()
        this.openListen()
    },

    closeUI() {
        for (let i = (this.content.children.length); i > 0; i--) {
            this.content.children[i - 1].getComponent('adIcon').clear()
            he.pool.putElement('adIcon', this.content.children[i - 1])
        }
        this.canUpdate = false
        this.scroll.scrollToTop()
        this.node.targetOff(this)
        this.closeListen()
        he.ad.changeBanner(false, this.bannerNode)
        // gg.uiManager.addPage(UI_Layer.Cover, {
        //     name: 'signPage',
        //     cover: true,
        //     // close: true,
        // })
    },

    closeServer() {

        switch (common.sdk_env) {
            case Sdk_Platform.WECHAT:
                let ver_info = he.local.getJson('ver_info')
                if (ver_info.banner_mistake) {
                    if (!this.isShowBanner) {
                        this.isShowBanner = true
                        this.scheduleOnce(this.bannerInit, 0.5)
                        this.scheduleOnce(this.hideBanner, 2)
                    } else {
                        this.uiBase.closePage()
                    }
                } else {
                    this.uiBase.closePage()
                }
                break
            default:
                this.uiBase.closePage()
                break
        }
    },

    bannerInit() {
        this.bannerNode.name = WX_AD_CONFIG.bannerId[0]
        /// 显示
        he.ad.changeBanner(true, this.bannerNode)
    },

    hideBanner() {
        this.bannerNode.name = WX_AD_CONFIG.bannerId[0]
        he.ad.changeBanner(false, this.bannerNode)
    },

    creatAdList(img) {
        for (let i = 0; i < img.length; i++) {
            let pre = he.pool.getElement('adIcon')
            pre.setPosition(cc.Vec2.ZERO)
            pre.active = true
            pre.setScale(1)
            pre.opacity = 255
            pre.parent = this.content
            let spt = pre.getComponent('adIcon')
            spt.init(img[i])
            if (i == 0) {
                this.posId = img[i].positionId
                this.crtId = img[i].creativeId
            }
        }
    },


    aniMation() {
        if (this.isTop) {
            this.scroll.scrollToBottom(0.2)
            this.isTop = false
        }
        else {
            this.isTop = true
            this.scroll.scrollToTop(0.2)
        }
    },

    update(dt) {
        if (this.canUpdate) {
            this.time += 1
            if (this.time == this.timeToScroll) {
                this.timeToScroll = this.randomNum(200, 300)
                this.aniMation()
                this.time = 0
            }
        }
    },
    scrollServer() {
        if (!this.scrollClick) {
            this.scrollClick = true
            if (wx.tmSDK) {
                wx.tmSDK.flowNavigate({
                    positionId: this.posId, // 广告位id, 请先使用该id获取推广创意列表
                    creativeId: +this.crtId,  // 传入获取到的creativeId
                }).then((newList) => {
                    // console.log('跳转成功or取消跳转（可根据特有返回值区分两种情况）')
                    // console.log('自动刷新列表：', newList)//返回最新列表 
                    this.scrollClick = true
                    this.clickNode.active = false
                    this.clickNode.off(cc.Node.EventType.TOUCH_START, this.scrollServer)
                }).catch((error) => {
                    console.log('跳转失败', error);
                    this.scrollClick = true
                })
            }
        }

    },

    //范围内的随机数
    randomNum(minNum, maxNum) {
        switch (arguments.length) {
            case 1:
                return parseInt(Math.random() * minNum + 1, 10);
                break;
            case 2:
                return parseInt(Math.random() * (maxNum - minNum + 1) + minNum, 10);
                break;
            default:
                return 0;
                break;
        }
    },

});
